﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Labthink.PCSoft.UniversalHost4.Model;
using Labthink.PCSoft.UniversalHost4.UserControls;
using Labthink.PCSoft.UniversalHost4.Utility;
using Report.Model;

namespace Report.Forms
{
    public partial class FrmSequence 
    {
        public List<FullData> Datas { get; set; }

        public List<SelectedData> Selecteds { get; set; }

        public List<SelectedData> Seqs { get; set; }

        private const int Col = 3;

        public FrmSequence()
        {
            InitializeComponent();
            Resources.MergedDictionaries.Add(ReportCommon.GetLan);
        }

        private void FrmSequence_OnLoaded(object sender, RoutedEventArgs e)
        {
            var rows = Math.Ceiling(Selecteds.Count / (double)Col).ToInt();
            for (int i = 0; i < rows; i++)
            {
                GridItems.RowDefinitions.Add(new RowDefinition { Height = new GridLength(50) });
            }

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < Col; j++)
                {
                    var di = i * Col + j;
                    if (Selecteds.Count > di)
                    {
                        var sd = Datas.SingleOrDefault(d => d.DataNo == Selecteds[di].Id);
                        if (sd != null)
                        {
                            var uc = new UcTextBox
                            {
                                Label = sd.Text,
                                Margin = new Thickness(5,5,5,5),
                                Tag = sd.DataNo,
                                Value = Selecteds[di].Sequence + "",
                                DataType = Labthink.PCSoft.UniversalHost4.UserControls.DataType.Int
                            };
                            GridItems.RegisterName("txt" + sd.DataNo, uc);
                            Grid.SetRow(uc, i);
                            Grid.SetColumn(uc, j);
                            GridItems.Children.Add(uc);
                        }
                    }
                }
            }
        }

        private void Input_LostFocus(object sender, RoutedEventArgs e)
        {
            var txt = sender as TextBox;
            if(txt== null)  return;
            var b = int.TryParse(txt.Text, out var _);
            if (!b) txt.Text = "0";
        }

        private void BtnCancel_OnClick(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void BtnReset_OnClick(object sender, RoutedEventArgs e)
        {
            Seqs = null;
            DialogResult = true;
            Close();
        }

        private void BtnOk_OnClick(object sender, RoutedEventArgs e)
        {
            Seqs = new List<SelectedData>();
            foreach (var sel in Selecteds)
            {
                if (GridItems.FindName("txt" + sel.Id) is UcTextBox txt)
                {
                    Seqs.Add(new SelectedData{Id = sel.Id, Sequence = txt.Value.ToInt()});
                }
            }
            DialogResult = true;
            Close();
        }
    }
}
